package com.RMP
{
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;
    import com.greensock.*;

    public class ProjVidThumbShell extends Sprite
    {
        public var myXMLList:XMLList;
        var counter:int;
        var projThumbnail:ProjThumbnail;
        var myURL:String;
        var fadeSpeed:Number;
        var nav:ProjPaginatedMenu;
        var listLength:int;
        var currVideoId:int;
        var videoShell:ProjVideoShell;
        var currNavId:int;
        public var hasSub:Boolean;
        var videoArray:Array;
        var myRoot:MovieClip;

        public function ProjVidThumbShell(param1:MovieClip, param2:Number, param3:XMLList) : void
        {
            trace("ProjVidThumbShell " + param3);
            myRoot = param1;
            fadeSpeed = param2;
            myXMLList = param3;
            listLength = myXMLList.length();
            videoArray = new Array();
            counter = 0;
            buildImageGrid();
            nav = new ProjPaginatedMenu(myRoot, fadeSpeed, counter);
            nav.addEventListener(PaginatedMenuClick.MENU_CLICK, menuClick, false, 0, true);
            myRoot.stage.addEventListener(Event.RESIZE, onResizer, false, 0, true);
            doResize();
            addChild(nav);
            currNavId = 0;
            updateGrid();
            videoShell = new ProjVideoShell(myRoot, fadeSpeed);
            videoShell.addEventListener(ClickArrowEvent.CLICK_ARROW, arrowClick, false, 0, true);
            videoShell.addEventListener(ClickCloseEvent.CLICK_CLOSE, closeClick, false, 0, true);
            videoShell.alpha = 0;
            if (hasSub == true)
            {
                nav.x = Math.round(10 * (myRoot.unitW + myRoot.gutterW));
            }
            else
            {
                nav.x = Math.round(9 * (myRoot.unitW + myRoot.gutterW));
            }
            nav.y = Math.round(myRoot.headerH + (myRoot.unitH + myRoot.gutterH) + -5 * myRoot.unitW / 75);
            return;
        }

        function arrowClick(event:ClickArrowEvent) : void
        {
            if (event.myDirection == "prev")
            {
                if (currVideoId == 0)
                {
                    currVideoId = listLength - 1;
                }
                else
                {
                    (currVideoId - 1);
                }
            }
            else if (currVideoId == (listLength - 1))
            {
                currVideoId = 0;
            }
            else
            {
                (currVideoId + 1);
            }
            videoShell.fadeOutImage(currVideoId);
            return;
        }

        public function doResize() : void
        {
            var _loc_1:int = 0;
            var _loc_2:Number = NaN;
            var _loc_3:int = 0;
            var _loc_4:* = undefined;
            var _loc_5:* = undefined;
            if (hasSub == true)
            {
                nav.x = Math.round(10 * (myRoot.unitW + myRoot.gutterW));
            }
            else
            {
                nav.x = Math.round(9 * (myRoot.unitW + myRoot.gutterW));
            }
            nav.y = Math.round(myRoot.headerH + (myRoot.unitH + myRoot.gutterH) + -5 * myRoot.unitW / 75);
            _loc_1 = myXMLList.length();
            _loc_2 = Math.floor(_loc_1 / 6);
            _loc_3 = 0;
            while (_loc_3 < _loc_1)
            {
                
                videoArray[_loc_3].x = Math.round(_loc_3 % 3 * 4 * (myRoot.unitW + myRoot.gutterW));
                _loc_4 = Math.floor(_loc_3 / 3);
                _loc_5 = _loc_4 % 2;
                videoArray[_loc_3].y = Math.round(myRoot.headerH + 2 * (myRoot.unitH + myRoot.gutterH) + _loc_5 * 2 * (myRoot.unitH + myRoot.gutterH));
                _loc_3++;
            }
            return;
        }

        public function fadeOutShell() : void
        {
            TweenMax.to(this, fadeSpeed, {alpha:0, overwrite:true, onComplete:function ()
            {
                visible = false;
                return;
            }
            });
            return;
        }

        function loadVideo(param1:int, param2:String, param3:String) : void
        {
            currVideoId = param1;
            addChild(videoShell);
            videoShell.fadeInShell();
            setTimeout(videoShell.loadVideo, fadeSpeed * 1000, param2, param3);
            return;
        }

        function onResizer(event:Event)
        {
            doResize();
            return;
        }

        function menuClick(param1:PaginatedMenuClick) : void
        {
            currNavId = param1.currentTarget.currNavId;
            updateGrid();
            return;
        }

        function closeClick(event:ClickCloseEvent) : void
        {
            videoShell.fadeOutShell();
            return;
        }

        function buildImageGrid() : void
        {
            var _loc_1:int = 0;
            _loc_1 = 0;
            while (_loc_1 < listLength)
            {
                
                videoArray[_loc_1] = new ProjThumbnail(myRoot, fadeSpeed, myXMLList[_loc_1], _loc_1, null);
                videoArray[_loc_1].buttonMode = true;
                videoArray[_loc_1].mouseChildren = true;
                videoArray[_loc_1].addEventListener(MouseEvent.CLICK, imageClick, false, 0, true);
                videoArray[_loc_1].addEventListener(MouseEvent.MOUSE_OVER, imageOver, false, 0, true);
                videoArray[_loc_1].addEventListener(MouseEvent.MOUSE_OUT, imageOut, false, 0, true);
                videoArray[_loc_1].showIcon = false;
                addChild(videoArray[_loc_1]);
                if (_loc_1 % 6 == 0)
                {
                    var _loc_3:* = counter + 1;
                    counter = _loc_3;
                }
                _loc_1++;
            }
            return;
        }

        function updateGrid() : void
        {
            var _loc_1:int = 0;
            _loc_1 = 0;
            while (_loc_1 < videoArray.length)
            {
                
                if (_loc_1 - currNavId * 6 >= 0 && _loc_1 - currNavId * 6 <= 5)
                {
                    videoArray[_loc_1].fadeInShell();
                }
                else
                {
                    videoArray[_loc_1].fadeOutShell();
                }
                _loc_1++;
            }
            doResize();
            return;
        }

        function imageOver(event:MouseEvent) : void
        {
            event.currentTarget.rollOverEvent();
            return;
        }

        function imageClick(event:MouseEvent) : void
        {
            loadVideo(event.currentTarget.myId, event.currentTarget.myXMLList.@url, event.currentTarget.myXMLList.longTitle);
            return;
        }

        public function fadeInShell() : void
        {
            visible = true;
            TweenMax.to(this, fadeSpeed, {alpha:1, delay:fadeSpeed, overwrite:true});
            return;
        }

        function imageOut(event:MouseEvent) : void
        {
            event.currentTarget.rollOutEvent();
            return;
        }

    }
}
